package cn.edu.zjicm.wordsnet_d.activity;

import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.data.DataManager;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.fragment.WordDetailFragment;
import cn.edu.zjicm.wordsnet_d.media.VoicePlayer;

public class WordDetailActivity extends WordBaseActivity {
	public static final int DIFFICULT_CHANGE_CODE = 0x100;
	private static final String BUNDLE_KEY_WORD_ID = "bundle_key_word_id";
	public static void startActivity(Context context, int wordId){
		Intent i = new Intent(context, WordDetailActivity.class);
		i.putExtra(BUNDLE_KEY_WORD_ID, wordId);
		((Activity)context).startActivityForResult(i, DIFFICULT_CHANGE_CODE);
	}
	
	private WordDetailFragment mDetailFragment;
	private OneWord oneWord;
	private boolean difficultChange=false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//设定调整音量为媒体音量
		setVolumeControlStream(AudioManager.STREAM_MUSIC); 
		
		setTitleBarStyle(WordBaseActivity.TITLE_BAR_STYLE_BACK_BUTTON);
		int wordId = getIntent().getIntExtra(BUNDLE_KEY_WORD_ID, -1);
		oneWord = DataManager.getInstance().getWord(this, wordId);
		if(oneWord == null){
			Toast.makeText(this, "单词不存在", Toast.LENGTH_SHORT).show();
			finish();
		}else{
			if(AppPreference.isAutoPronunciation(this)){
				VoicePlayer.getInstance(this).play(oneWord);
			}
		}
		
		setBackButton(backListener);
		setTitleText(oneWord.getLemma());
		setRightButton0(oneWord.isDifficulty(),onClick);
		setContentView(R.layout.activity_word_detail);
		
		mDetailFragment = new WordDetailFragment(oneWord);
	//	mDetailFragment.setForSearchView(false);	
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.add(R.id.word_detail_fragment_container, mDetailFragment);
		ft.commit();			
	}
	
	private OnClickListener onClick=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			difficultChange=true;
			if(oneWord.isDifficulty())
			{
				oneWord.setDifficulty(false, WordDetailActivity.this);
				Toast.makeText(WordDetailActivity.this, R.string.set_to_difficult_hint2, Toast.LENGTH_SHORT).show();
			}
			else
			{
				oneWord.setDifficulty(true, WordDetailActivity.this);
				Toast.makeText(WordDetailActivity.this, R.string.set_to_difficult_hint1, Toast.LENGTH_SHORT).show();
			}
			setRightButton0(oneWord.isDifficulty());
		}
	};

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
		
	//	mDetailFragment.showAddButton();			//显示添加移出生词本模式	
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			if(difficultChange)
			{
				setResult(Activity.RESULT_OK);
			}
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);

	}
	
	
	private OnClickListener backListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(difficultChange)
			{
				setResult(Activity.RESULT_OK);
			}
			finish();
		}
	};
}
